package com.shujia.flink.tf;

import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

public class Demo04KeyBy {
    public static void main(String[] args) throws Exception {
        // 用于就数据流分组，让相同的Key进入到同一个任务中进行处理，后续可以跟聚合操作
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        DataStream<String> ds = env.socketTextStream("master", 8888);

        KeyedStream<String, String> keyByDS = ds.keyBy(new KeySelector<String, String>() {
            @Override
            public String getKey(String value) throws Exception {
                return value;
            }
        });
        keyByDS.print();

        ds.keyBy(value -> value.toLowerCase(), Types.STRING).print();
        ds.keyBy(String::toLowerCase, Types.STRING).print();


        env.execute();

    }
}
